package com.xyh.controller;

import com.xyh.entity.YxcCar;
import com.xyh.service.YxcCarService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
public class YxcCarController {

    @Resource
    private YxcCarService yxcCarService;

    @RequestMapping("/deleteOneCar")
    public String deleteOneCar(HttpServletRequest request, RedirectAttributes attributes){
        int car_id = Integer.parseInt(request.getParameter("car_id"));
        String owner_telephone = request.getParameter("owner_telephone");
        int flag = yxcCarService.deleteOneCar(car_id,owner_telephone);
        if (flag == 0) throw new RuntimeException("删除失败");
        attributes.addAttribute("login_telephone",owner_telephone);
        attributes.addAttribute("situation",1);
        return "redirect:to_yxc_main";
    }

    @RequestMapping("/to_yxc_car_add")
    public String toYxcCarAdd(HttpServletRequest request, Model model){
        String car_owner = request.getParameter("car_owner");
        String owner_telephone = request.getParameter("owner_telephone");

        model.addAttribute("car_owner",car_owner);
        model.addAttribute("owner_telephone",owner_telephone);
        return "yxc_car_add";
    }

    @RequestMapping("/yxc_car_add")
    public String YxcCarAdd(YxcCar yxcCar, RedirectAttributes attributes){
//        String car_number = request.getParameter("car_number");
//        String car_owner = request.getParameter("car_owner");
//        String owner_telephone = request.getParameter("owner_telephone");

        int flag = yxcCarService.addOneCarByPhone(yxcCar);
        if (flag == 0) throw new RuntimeException("添加失败");
        attributes.addAttribute("login_telephone",yxcCar.getOwner_telephone());
        attributes.addAttribute("situation",1);
        return "redirect:to_yxc_main";
    }

}
